﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using Nissen_Common;
using Nissen_Dao;
using Nissen_Model;

namespace Nissen_StartUp
{
	public class LoginFormService
	{
		/// <summary>
		/// 取得计算机对应登录信息
		/// </summary>
		/// <returns></returns>
		public Machine GetMachine()
		{
			try
			{
				string matchineName = System.Net.Dns.GetHostName();

				MachineDao machineDao = new MachineDao();
				IList<Machine> machines = machineDao.GetMachineByName(matchineName);

				if (machines.Count == 0)
				{
					return CreatMachineMapSSO(matchineName);
				}
				else
				{
					return machines[0];
				}
			}
			catch (Exception ex)
			{
				
				throw ex;
			}
			
		}

		/// <summary>
		/// 取得机器与SSO关联信息
		/// </summary>
		/// <param name="machineName"></param>
		/// <returns></returns>
		private Machine CreatMachineMapSSO(string machineName)
		{
			NissenSSODao nissenSsoDao = new NissenSSODao();
			Machine machine = new Machine();
			machine.MachineName = machineName;

			IList<NissenSSO> nissenSsos = nissenSsoDao.GetNissenSSOUnUsing();
			if (nissenSsos.Count>0)
			{
				machine.NissenSSO = nissenSsos[0];

			}
			else
			{
				throw new Exception("获得作业账户失败，请联系管理员！");
			}
			MachineDao machineDao = new MachineDao();
			machineDao.SaveOrUpdate(machine);

			return machine;
		}

		/// <summary>
		/// 通过登录用户分配操作用户
		/// </summary>
		/// <param name="loginSSO"></param>
		/// <returns></returns>
		public NissenSSO GetNissenSSO(string loginSSO,string ssoMin, string ssoMax,string loginName)
		{
			WorkerDao workerDao = new WorkerDao();
			IList<Worker> workers = workerDao.GetWorkerByWorkerSSO(loginSSO);
			if (workers.Count == 0)
			{
				return CreatWorkerMapSSO(loginSSO, ssoMin, ssoMax, loginName);
			}
			else
			{
				return workers[0].NissenSSO;
			}
		}

		private NissenSSO CreatWorkerMapSSO(string loginSSO, string ssoMin, string ssoMax, string loginName)
		{
			NissenSSODao nissenSsoDao = new NissenSSODao();

			WorkerNissenMapLogDao workerNissenMapLogDao = new WorkerNissenMapLogDao();

            NissenSSO returnNissenSSO = new NissenSSO();

            Worker worker = new Worker();
		    worker.WorkerSSO = loginSSO;

            returnNissenSSO = nissenSsoDao.GetNissenSSOUnUsingForWorker(worker, ssoMin, ssoMax);

            if (returnNissenSSO == null)
			{
				throw new Exception("获得作业账户失败，请联系管理员！");
			}

            workerNissenMapLogDao.SaveWorkerNissenMapLog(worker, loginName);

            return returnNissenSSO;

		}
		

		public WorkerCache GetWorkerCache(string sso)
		{
			WorkerCacheDao workerCacheDao = new WorkerCacheDao();

			return workerCacheDao.GetWorkerCacheBySSO(sso);
		}

		public void SaveOrUpdateWorkerCache(string sso,string name)
		{
			WorkerCache workerCache = new WorkerCache();
			workerCache.WorkerSSO = sso;
			workerCache.WorkerName = name;

			WorkerCacheDao workerCacheDao = new WorkerCacheDao();

			 workerCacheDao.SaveOrUpdateWorkerCache(workerCache);
			
		}
	}
}
